library(here)
library(tidyverse)
library(countrycode)
library(haven)



# LAPOP Data from 2010 to 2018
load(here("data/lapop_slim.RData"))

# SHAPING GLOBAL POPULISM DATA ----------------------------------------------

# Read in global populism data
global_populism <- read.csv(here("data/GPD_20190625.csv"))

# Keep LATAM countries and relevant variables 
global_populism <- global_populism %>%  
  filter(region == "Latin America & Caribbean" & yearend > 2005) %>% 
  select(country, leader, term, startofterm, 
         endofterm, yearbegin, yearend, pop.speech = totalaverage) %>% 
  unique.data.frame() %>% 
  arrange(country, yearbegin)


# Create Country Code in Global Populism Data
global_populism <- global_populism %>% 
  mutate(iso3c = factor(countrycode(country, 
                             origin = "country.name", 
                             destination = "iso3c")))

# ISO3c for Countries  
countries <-  unique(global_populism$iso3c)

# Make Evo Morales naming constant
global_populism$leader[global_populism$leader=="Morales"] <-  "Evo Morales"


# DESTINATION DATAFRAME ----------------------------------------------

# Create Data Frame for Merged Data 
lapop_winners <- data.frame(wave = rep(seq(2010, 2018, by = 2), times = 18),  #waves from 2010 to 2018
                            iso3c = rep(countries, each = 5))  # for each country 

# Grab the LAPOP survey years from the survey data 
srvy_years <- lapop.slim %>% 
  select(wave, iso3c, 
         survey_year = year) %>% 
  unique.data.frame()


# Join in LAPOP survey years 
lapop_winners <- left_join(lapop_winners, srvy_years)



# JOIN DATAFRAMES -------------------------------------------
lapop_winners_merge <- cross_join(lapop_winners, global_populism) %>%  # keep all combinations of x and y 
  filter(iso3c.x == iso3c.y,  # keep only matching country names
         survey_year >= yearbegin,  # keep if survey year is >= the start of the presidential term
         survey_year <= yearend)  # keep if survey year is <=  the end the presidential term


# Just keep one iso3c variables 
lapop_winners_merge <- lapop_winners_merge %>% 
  select(-iso3c.y) %>% 
  rename(iso3c = iso3c.x)


# Some countries year combinations are still ambiguous, e.g. 
lapop_winners_merge %>% 
  filter(iso3c == "CHL", wave == 2010)

lapop_winners_merge %>% 
  group_by(iso3c, wave) %>% 
  count() %>% 
  filter(n > 1)


# Drop incorrect country-year-leader combinations that remain in the data 
# Keeping the presidents asked about in LAPOP's vb3 question
lapop_winners_merge <- lapop_winners_merge %>% 
  filter( 
    !(iso3c == "CHL" & wave == 2010 & leader == "Michelle Bachelet"), # CHL-2010 asks about Piñera, drop Bachelet
    !(iso3c == "CHL" & wave == 2014 & leader == "Sebastián Piñera"),  # CHL-2014 asks about Bachelet, drop Piñera
    !(iso3c == "COL" & wave == 2010 & leader == "Juan Manuel Santos"), # COL-2010 asks about Uribe, drop Santos 
    !(iso3c == "COL" & wave == 2014 & leader == "Juan Manuel Santos" & term == 2), #COL_2014 is during term 1, drop term 2
    !(iso3c == "COL" & wave == 2018 & leader == "Juan Manuel Santos"), # COL 2018 asks about Duque, not in the GPD
    !(iso3c == "CRI" & wave == 2010 & leader == "Laura Chinchilla Miranda"), # CRI-2010 asks about Arias, drop Miranda
    !(iso3c == "CRI" & wave == 2014 & leader == "Laura Chinchilla Miranda"), # CRI-2010 asks about Solis, drop Miranda
    !(iso3c == "CRI" & wave == 2018 & leader == "Luis Guillermo Solis"), # 2018 asks about Quesada, not in the GDP
    !(iso3c == "DOM" & wave == 2012 & leader == "Danilo Medina"),  # DOM-2012 asks about Fernandez, drop Medina
    !(iso3c == "DOM" & wave == 2016 & leader == "Danilo Medina" & term == 1), # DOM 2016 is during term 2, drop term 1
    !(iso3c == "PAN" & wave == 2014 & leader == "Juan Carlos Varela"), # PAN 2014 asks about Martineli, drop Verela
    !(iso3c == "PRY" & wave == 2012 & leader == "Fernando Lugo" & yearbegin == 2008),  # Lugo repeats but the pop.speech value is the same 
    !(iso3c == "SLV" & wave == 2014 & leader == "Mauricio Funes"), # SLV-2014 asks about Ceren, drop Funes
    !(iso3c == "URY" & wave == 2010 & leader == "Tabaré Vásquez") #URY 2010 asks about Mujica, drop Vasquez 
    )



# VB3_n Code for Presidential Supporters -----------------------------------


lapop_winners_merge  <- lapop_winners_merge %>% 
  mutate(vb3code = case_when(
    
    wave == 2010 & iso3c == "ARG" ~ 1701,
    wave == 2012 & iso3c == "ARG" ~ 1701,
    wave == 2014 & iso3c == "ARG" ~ 1701,
    wave == 2016 & iso3c == "ARG" ~ 1701,
    wave == 2018 & iso3c == "ARG" ~ 1701,
    
    wave == 2010 & iso3c == "BOL" ~ 1002,
    wave == 2012 & iso3c == "BOL" ~ 1002,
    wave == 2014 & iso3c == "BOL" ~ 1002,
    wave == 2016 & iso3c == "BOL" ~ 1001,
    wave == 2018 & iso3c == "BOL" ~ 1001,
    
    wave == 2010 & iso3c == "BRA" ~ 1501,
    wave == 2012 & iso3c == "BRA" ~ 1501,
    wave == 2014 & iso3c == "BRA" ~ 1501,
    wave == 2016 & iso3c == "BRA" ~ 1501,
    wave == 2018 & iso3c == "BRA" ~ 1501,
    
    wave == 2010 & iso3c == "CHL" ~ 1302,
    wave == 2012 & iso3c == "CHL" ~ 1302,
    wave == 2014 & iso3c == "CHL" ~ 1305,
    wave == 2016 & iso3c == "CHL" ~ 1305,
    wave == 2018 & iso3c == "CHL" ~ 1301,
    
    wave == 2010 & iso3c == "COL" ~ 803,
    wave == 2012 & iso3c == "COL" ~ 804,
    wave == 2014 & iso3c == "COL" ~ 804,
    wave == 2016 & iso3c == "COL" ~ 804,
    wave == 2018 & iso3c == "COL" ~ 801,
    
    wave == 2010 & iso3c == "CRI" ~ 602,
    wave == 2012 & iso3c == "CRI" ~ 602,
    wave == 2014 & iso3c == "CRI" ~ 603,
    wave == 2016 & iso3c == "CRI" ~ 603,
    wave == 2018 & iso3c == "CRI" ~ 601,
    
    wave == 2010 & iso3c == "DOM" ~ 2101,
    wave == 2012 & iso3c == "DOM" ~ 2101,
    wave == 2014 & iso3c == "DOM" ~ 2101,
    wave == 2016 & iso3c == "DOM" ~ 2101,
    wave == 2018 & iso3c == "DOM" ~ 2101,
    
    wave == 2010 & iso3c == "ECU" ~ 901,
    wave == 2012 & iso3c == "ECU" ~ 901,
    wave == 2014 & iso3c == "ECU" ~ 901,
    wave == 2016 & iso3c == "ECU" ~ 901,
    wave == 2018 & iso3c == "ECU" ~ 901,
    
    wave == 2010 & iso3c == "GTM" ~ 201,
    wave == 2012 & iso3c == "GTM" ~ 206,
    wave == 2014 & iso3c == "GTM" ~ 206,
    wave == 2016 & iso3c == "GTM" ~ 201,
    wave == 2018 & iso3c == "GTM" ~ 201,
    
    wave == 2010 & iso3c == "HND" ~ 404,
    wave == 2012 & iso3c == "HND" ~ 404,
    wave == 2014 & iso3c == "HND" ~ 401,
    wave == 2016 & iso3c == "HND" ~ 401,
    wave == 2018 & iso3c == "HND" ~ 401,
    
    wave == 2010 & iso3c == "MEX" ~ 101,
    wave == 2012 & iso3c == "MEX" ~ 101,
    wave == 2014 & iso3c == "MEX" ~ 101,
    wave == 2016 & iso3c == "MEX" ~ 101,
    wave == 2018 & iso3c == "MEX" ~ 101,
    
    wave == 2010 & iso3c == "NIC" ~ 502,
    wave == 2012 & iso3c == "NIC" ~ 502,
    wave == 2014 & iso3c == "NIC" ~ 502,
    wave == 2016 & iso3c == "NIC" ~ 502,
    wave == 2018 & iso3c == "NIC" ~ 501,
    
    wave == 2010 & iso3c == "PAN" ~ 702,
    wave == 2012 & iso3c == "PAN" ~ 702,
    wave == 2014 & iso3c == "PAN" ~ 702,
    wave == 2016 & iso3c == "PAN" ~ 701,
    wave == 2018 & iso3c == "PAN" ~ 701,
    
    wave == 2010 & iso3c == "PER" ~ NA_integer_,
    wave == 2012 & iso3c == "PER" ~ 1101,
    wave == 2014 & iso3c == "PER" ~ 1101,
    wave == 2016 & iso3c == "PER" ~ 1101,
    wave == 2018 & iso3c == "PER" ~ 1101, 
    
    wave == 2010 & iso3c == "PRY" ~ 1201,
    wave == 2012 & iso3c == "PRY" ~ 1201,
    wave == 2014 & iso3c == "PRY" ~ 1201,
    wave == 2016 & iso3c == "PRY" ~ 1201,
    wave == 2018 & iso3c == "PRY" ~ 1201,
    
    wave == 2010 & iso3c == "SLV" ~ 302,
    wave == 2012 & iso3c == "SLV" ~ 302,
    wave == 2014 & iso3c == "SLV" ~ 302,
    wave == 2016 & iso3c == "SLV" ~ 302,
    wave == 2018 & iso3c == "SLV" ~ 302,
    
    wave == 2010 & iso3c == "URY" ~ 1401,
    wave == 2012 & iso3c == "URY" ~ 1401,
    wave == 2014 & iso3c == "URY" ~ 1401,
    wave == 2016 & iso3c == "URY" ~ 1401,
    wave == 2018 & iso3c == "URY" ~ 1401,
    
    wave == 2010 & iso3c == "VEN" ~ 1601,
    wave == 2012 & iso3c == "VEN" ~ 1601,
    wave == 2014 & iso3c == "VEN" ~ 1601,
    wave == 2016 & iso3c == "VEN" ~ 1601,
    
  ))



# Vb11 Code for Partisan alignment with executive ---------------------- 

lapop_winners_merge  <- lapop_winners_merge %>% 
  mutate(vb11code = case_when(
    
    wave == 2010 & iso3c == "ARG" ~ 1702,
    wave == 2012 & iso3c == "ARG" ~ 1701,
    wave == 2014 & iso3c == "ARG" ~ 1702,
    wave == 2016 & iso3c == "ARG" ~ 1705,
    wave == 2018 & iso3c == "ARG" ~ 1705,
    
    wave == 2010 & iso3c == "BOL" ~ 1002,
    wave == 2012 & iso3c == "BOL" ~ 1002,
    wave == 2014 & iso3c == "BOL" ~ 1001,
    wave == 2016 & iso3c == "BOL" ~ 1001,
    wave == 2018 & iso3c == "BOL" ~ 1001,
    
    wave == 2010 & iso3c == "BRA" ~ 1501,
    wave == 2012 & iso3c == "BRA" ~ 1501,
    wave == 2014 & iso3c == "BRA" ~ 1501,
    wave == 2016 & iso3c == "BRA" ~ 1501,
    wave == 2018 & iso3c == "BRA" ~ 1514,
    
    wave == 2010 & iso3c == "CHL" ~ 1304,
    wave == 2012 & iso3c == "CHL" ~ 1304,
    wave == 2014 & iso3c == "CHL" ~ 1301,
    wave == 2016 & iso3c == "CHL" ~ 1301,
    wave == 2018 & iso3c == "CHL" ~ 1304,
    
    wave == 2010 & iso3c == "COL" ~ 802,
    wave == 2012 & iso3c == "COL" ~ 804,
    wave == 2014 & iso3c == "COL" ~ 804,
    wave == 2016 & iso3c == "COL" ~ 804,
    wave == 2018 & iso3c == "COL" ~ 829,
    
    wave == 2010 & iso3c == "CRI" ~ 602,
    wave == 2012 & iso3c == "CRI" ~ 602,
    wave == 2014 & iso3c == "CRI" ~ 603,
    wave == 2016 & iso3c == "CRI" ~ 603,
    wave == 2018 & iso3c == "CRI" ~ 603,
    
    wave == 2010 & iso3c == "DOM" ~ 2102,
    wave == 2012 & iso3c == "DOM" ~ 2102,
    wave == 2014 & iso3c == "DOM" ~ 2101,
    wave == 2016 & iso3c == "DOM" ~ 2102,
    wave == 2018 & iso3c == "DOM" ~ 2102,
    
    wave == 2010 & iso3c == "ECU" ~ 913,
    wave == 2012 & iso3c == "ECU" ~ 913,
    wave == 2014 & iso3c == "ECU" ~ 913,
    wave == 2016 & iso3c == "ECU" ~ 913,
    wave == 2018 & iso3c == "ECU" ~ 913,
    
    wave == 2010 & iso3c == "GTM" ~ 201,
    wave == 2012 & iso3c == "GTM" ~ 206,
    wave == 2014 & iso3c == "GTM" ~ 206,
    wave == 2016 & iso3c == "GTM" ~ 201,
    wave == 2018 & iso3c == "GTM" ~ 201,
    
    wave == 2010 & iso3c == "HND" ~ 401,
    wave == 2012 & iso3c == "HND" ~ 401,
    wave == 2014 & iso3c == "HND" ~ 401,
    wave == 2016 & iso3c == "HND" ~ 401,
    wave == 2018 & iso3c == "HND" ~ 401,

    wave == 2010 & iso3c == "MEX" ~ 101,
    wave == 2012 & iso3c == "MEX" ~ 101,
    wave == 2014 & iso3c == "MEX" ~ 102,
    wave == 2016 & iso3c == "MEX" ~ 102,
    wave == 2018 & iso3c == "MEX" ~ 109,
    
    wave == 2010 & iso3c == "NIC" ~ 502,
    wave == 2012 & iso3c == "NIC" ~ 502,
    wave == 2014 & iso3c == "NIC" ~ 502,
    wave == 2016 & iso3c == "NIC" ~ 502,
    wave == 2018 & iso3c == "NIC" ~ 502,
    
    wave == 2010 & iso3c == "PAN" ~ 702,
    wave == 2012 & iso3c == "PAN" ~ 702,
    wave == 2014 & iso3c == "PAN" ~ 702,
    wave == 2016 & iso3c == "PAN" ~ 702,
    wave == 2018 & iso3c == "PAN" ~ 702,
    
    wave == 2010 & iso3c == "PER" ~ 1101,
    wave == 2012 & iso3c == "PER" ~ 1101,
    wave == 2014 & iso3c == "PER" ~ 1101,
    wave == 2016 & iso3c == "PER" ~ 1101,
    wave == 2018 & iso3c == "PER" ~ 1101,
    
    wave == 2010 & iso3c == "PRY" ~ 1211,
    wave == 2012 & iso3c == "PRY" ~ 1211,
    wave == 2014 & iso3c == "PRY" ~ 1201,
    wave == 2016 & iso3c == "PRY" ~ 1201,
    wave == 2018 & iso3c == "PRY" ~ 1201,
    
    wave == 2010 & iso3c == "SLV" ~ 302,
    wave == 2012 & iso3c == "SLV" ~ 302,
    wave == 2014 & iso3c == "SLV" ~ 301,
    wave == 2016 & iso3c == "SLV" ~ 301,
    wave == 2018 & iso3c == "SLV" ~ 301,
    
    wave == 2010 & iso3c == "URY" ~ 1402,
    wave == 2012 & iso3c == "URY" ~ 1401,
    wave == 2014 & iso3c == "URY" ~ 1401,
    wave == 2016 & iso3c == "URY" ~ 1401,
    wave == 2018 & iso3c == "URY" ~ 1401,
    
    wave == 2010 & iso3c == "VEN" ~ 1611,
    wave == 2012 & iso3c == "VEN" ~ 1611,
    wave == 2014 & iso3c == "VEN" ~ 1611,
    wave == 2016 & iso3c == "VEN" ~ 1601,
    
  ))


# Save to CSV ----------------

write.csv(lapop_winners_merge, 
          file = here("data/lapop_winners.csv"), 
          row.names = F)




















